#!/usr/bin/env bash
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :sysbench 压测脚本
# Platform :RedHatEL7.x Based Platform
# Version  :1.12
# Date     :2022-03-31
# Author   :mugoLH
# Contact  :hxsaj@126.com
# 使用方式  :
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

info_p(){  printf "\e[1;32;40m%-12s %-1s\n\e[0m" "${1}" "${2}"  >&1 ;}
tips_p(){  printf "\e[1;36;40m%-5s %-1s\n\e[0m" "${1}" "${2}"  >&1 ;}
error_p(){ printf "\e[1;35;40m%-12s %-1s\n\e[0m" "${1}" "${2}"  >&1 ;}


#  变量列表 List of common variables
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

#  函数列表 Function list
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
sysbench_test(){
	get_DB_HOST(){
		if [ $# -eq 0 ];then
			read -p  "请输入DB主机地址：" DB_HOST
		else
			DB_HOST=${1}
		fi
		if ping -c1 -W1 ${DB_HOST} >/dev/null 2>&1;then
			export DB_HOST
		else
			tips_p "[ ERROR ]:" "主机不可访问，请检查后重试！"
			get_DB_HOST
		fi
	}
	get_DB_PORT(){
		if [ $# -eq 0 ];then
			read -p  "请输入DB服务端口：" DB_PORT
		else
			DB_PORT=${1}
		fi
		export DB_PORT
	}
	get_DB_USER(){
		if [ $# -eq 0 ];then
			read -p  "请输入DB的用户：" DB_USER
		else
			DB_USER=${1}
		fi
		export DB_USER
	}
	get_DB_PWD(){
		if [ $# -eq 0 ];then
			read -p  "请输入DB的密码：" DB_PWD
		else
			DB_PWD=${1}
		fi
		export DB_PWD
	}
	create_data(){
		if [ $# -eq 0 ];then
			get_DB_HOST
			get_DB_PORT
			get_DB_USER
			get_DB_PWD
		fi
	}

	if [ $# -eq 0 ];then
		get_DB_HOST
		get_DB_PORT
		get_DB_USER
		get_DB_PWD
		
	fi
}
sysbench \
/usr/share/sysbench/oltp_read_only.lua \
--db-driver=mysql \
--threads=10 \
--mysql-host=127.0.0.1 \
--mysql-port=3306 \
--mysql-user=root \
--mysql-password="594hxs" \
--mysql-db=sbtest1 \
--table-size=1000000 \
--tables=10 \
--report-interval=1 \
--threads=40 \
--time=60 \
run

sysbench \
/usr/share/sysbench/oltp_common.lua \
--db-driver=mysql \
--threads=10 \
--mysql-host=127.0.0.1 \
--mysql-port=3306 \
--mysql-user=root \
--mysql-password="594hxs" \
--mysql-db=sbtest1 \
--table-size=1000000 \
--tables=10 \
--report-interval=1 \
prepare


sysbench \
/usr/share/sysbench/oltp_read_write.lua \
--db-driver=mysql \
--threads=10 \
--mysql-host=127.0.0.1 \
--mysql-port=3306 \
--mysql-user=root \
--mysql-password="594hxs" \
--mysql-db=sbtest1 \
--table-size=1000000 \
--tables=10 \
--report-interval=1 \
--threads=40 \
--time=600 \
run